package jlfm.lastfm.apps;

import jlfm.lastfm.client.LastFmLoginException;
import jlfm.lastfm.client.impl.advanced.Credential;
import jlfm.lastfm.client.impl.simple.ProxyConfiguration;
import jlfm.lastfm.client.impl.simple.SimpleLastFmClient;
import jlfm.lastfm.client.impl.simple.StreamHandler;
import jlfm.lastfm.station.LastFmStation;

public class LastFmStreamSupporter {

	private Credential credential;

	private LastFmStation station;

	private SimpleLastFmClient c;

	private StreamHandler streamHandler = null;

	private EncapsulatingStreamHandler streamHandlerWrapper;

	public LastFmStreamSupporter(Credential loginCredential,
			LastFmStation station, StreamHandler streamHandler) {
		this.credential = loginCredential;
		this.station = station;
		this.streamHandler = streamHandler;
		c = new SimpleLastFmClient();
	}

	public void setProxyConfiguration(ProxyConfiguration proxyConfiguration) {
		c.setProxyConfiguration(proxyConfiguration);
	}

	public void startStreaming() throws LastFmLoginException {
		streamHandlerWrapper = new EncapsulatingStreamHandler(c, streamHandler);
		c.login(credential);
		c.tuneIn(station);
	}

	public void stopStreaming() {
		streamHandlerWrapper.setEnd(true);
	}

	public void setStation(LastFmStation station) {
		this.station = station;
	}

	public StreamHandler getStreamHandler() {
		return streamHandler;
	}
}
